package library.user_creator;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import library.TextPane;

import user_client.LibrarianUserClient;

public class UserClientCreatorPane extends JPanel {

	private String name = "";
	private String surname = "";
	private Integer userId = null;
	private String adres = "";
	private JPanel mainArea = null;

	private TextPane infoEntry = null;
	private CreatorReturnedInformationPane returnPane = 
			new CreatorReturnedInformationPane();
	private CreatorPane creator = new CreatorPane();
	private JButton showCreator = new JButton("dodaj czytelnika");
	private LibrarianUserClient client;
	
	
	
	
	public UserClientCreatorPane(JPanel mainArea, LibrarianUserClient client) {
		
		this.client = client;
		this.mainArea = mainArea;
		creator.setVisible(true);
		showCreator.setVisible(true);
		showCreator.addActionListener(new ShowCreatorActionListener());
		this.add(showCreator);
	}
	
	private class ShowCreatorActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
				mainArea.removeAll();
				mainArea.add(creator);		
				mainArea.revalidate();
				mainArea.repaint();
		}
	}

	private class CreatorPane extends JPanel {

		
		
		public CreatorPane() {
			this.setLayout(new GridLayout(4, 1));
			String[] names = new String[3];
			names[0] = "name";
			names[1] = "surname";
			names[2] = "adres";
			/*names[3] = "login";
			names[4] = "temporary password";
			*/
			infoEntry = new TextPane(names, new JTextField(), 
					new JTextField(), new JTextField());
			
			this.add(infoEntry);
			this.add(returnPane);
			
			JPanel decisionPanel = new JPanel(new GridLayout(1, 2));
			JButton createUser = new JButton("zatwierdź");
			createUser.addActionListener(new CreateUserActionListener());
			JButton abort = new JButton("anuluj");
			abort.addActionListener(new AbortCreatorActionListener());
			decisionPanel.add(createUser);
			decisionPanel.add(abort);
			this.add(decisionPanel);
		}

		private class CreateUserActionListener implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String name = infoEntry.getField("name").getText();
				String surname = infoEntry.getField("surname").getText();
				String adres = infoEntry.getField("adres").getText();
				
				
				/*JPasswordField passField1 = (JPasswordField) infoEntry.getField("password");
				JPasswordField passField2 = (JPasswordField) infoEntry.getField("password again");
				String pass1 = String.valueOf(passField1.getPassword());
				String pass2 = String.valueOf(passField2.getPassword());				
				*/
				try {
					client.addClient(returnPane, name, surname, adres);
				} catch (SQLException e1) {
					System.out.println(e1.getMessage());
					// TODO Auto-generated catch block
					
					System.out.println("Błąd !!!");
				}
				infoEntry.clearTextFields();
		/*		tokenResult.setText(userToken);
				System.out.println(userToken);
		*/	}

		}

		private class AbortCreatorActionListener implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				mainArea.removeAll();	
				UserClientCreatorPane.this.revalidate();
				CreatorPane.this.revalidate();
				mainArea.repaint();
				
			}
		}
	}
}
